// //获取url里的参数
// function getQueryVariable(variable)
// {
//        var query = window.location.search.substring(1);
//        var vars = query.split("&");
//        for (var i=0;i<vars.length;i++) {
//                var pair = vars[i].split("=");
//                if(pair[0] == variable){return pair[1];}
//        }
//        return(false);
// }

//获取url内的中文参数
function UrlSearch() {
	var name, value;
	var str = decodeURI(location.href); //取得解码后的地址栏
	var num = str.indexOf("?")
	str = str.substr(num + 1);

	var arr = str.split("&"); //各个参数放到数组里
	for (var i = 0; i < arr.length; i++) {
		num = arr[i].indexOf("=");
		if (num > 0) {
			name = arr[i].substring(0, num);
			value = arr[i].substr(num + 1);
			this[name] = value;
		}
	}
}

var obj = new UrlSearch();
//获取物品名称
wupianname = obj.wupian;
// console.log(wupianname);

var img;

$.ajax({
	url: "shuju.json",
	success: function(data) {
		$.each(data, function(index, item) {
			if (item.name == wupianname) {
				$('.wupianname').text(`${item.name}`);
				$('.shoujia').html(`${item.shoujia}元 <span>${item.dazheshoujia}元</span>`).next().eq(0).text(`总计：${item.shoujia}元`);
				$('.fubiaoti').text(`${item.miaoshu}`);
				$('.jiage').html(`${item.shoujia}元 <span class='yuanjia'>${item.dazheshoujia}元</span>`);

				if (wupianname != '全面屏电视E43K'){
					$('#mainRight div:nth-of-type(4)').css('display', 'none');
					$('#bananer').html(`<img src=${item.img}>`)
				}
				img=item.img;
				// console.log($('#mainRight div:nth-of-type(5)'))
			}
		})
	}
})



//点击加入购物车
$('.jiaru').click(function() {
	//获取物品价格
	var shoujia = parseFloat($('.shoujia').text());
	//获取图片路径
	img=img;
	$.ajax({
		url: "php/buy.php",
		data: {
			"img": img,
			"wupian": wupianname,
			"shoujia": shoujia
		},
		success: function(data) {
			if (data == 1) {
				alert('加入购物车成功')
			} else {
				alert('加入失败')
			}
		}
	})
})


//轮播图
var mySwiper = new Swiper('.swiper-container', {
			// direction: 'vertical', // 垂直切换选项
			loop: true, // 循环模式选项

			// 如果需要分页器
			pagination: {
				el: '.swiper-pagination',
				// type: 'custom',

			},

			// 如果需要前进后退按钮
			navigation: {
				nextEl: '.swiper-button-next',
				prevEl: '.swiper-button-prev',
			},

			autoplay: {
				autoplay: true,
				stopOnLastSlide: true,
			}
		})
